import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {
    public static <T> List<T> readData(String path){
        List<T> dataList=new ArrayList<>();
        File file=new File(path);
        if(file.exists()){
            try (InputStream is=new FileInputStream(file);
                 ObjectInputStream ois=new ObjectInputStream(is)){
                dataList=(List<T>) ois.readObject();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return dataList;
    }
    public static <T> boolean writeData(String path,List<T> dataList){
        File file =new File(path);
        File parent=file.getParentFile();
        boolean success=true;
        if(!parent.exists()){
            parent.mkdirs();
            try( OutputStream os=new FileOutputStream(file);
                 ObjectOutputStream oos=new ObjectOutputStream(os)){
                oos.writeObject(dataList);
            }catch(IOException e){
                success=false;
                e.printStackTrace();
            }
        }
        return success;
    }
}
